package com.stars.home.vmodel

import android.app.Activity
import android.app.Application
import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.widget.LinearLayout
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.hwangjr.rxbus.RxBus
import com.stars.home.activity.PayActivity
import com.stars.home.config.ConfigRx
import com.stars.home.config.MIntentAction
import com.stars.home.model.CompreDetailsBean
import com.stars.home.utils.DecimalUtils
import com.stars.home.utils.PopuGuideUtils
import com.stars.home.utils.net.NetEngine
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import com.trailer.base.utils.B
import com.trailer.base.utils.DialUtils
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch
import java.util.*

//订单状态
class WayDetailsVModel(application: Application) : BViewModel<BRepository>(application)  {

    var isUp = true


    var order_id:String?= ""


    var bean= MutableLiveData<CompreDetailsBean>()
    var state= MutableLiveData<Int>()//订单状态

    var change= MutableLiveData<Int>()

    var index:String?="" //刷新消息列表用的

    var ll:LinearLayout?=null


    fun isShowChat(state:Int,type:Int):Int{

        if(state==-1||state==0||state==1||state==6){
            return View.GONE
        }
        if(type==1||type==2||type==3){
            return View.GONE
        }
        return View.VISIBLE
    }

    fun isShowPhone(state:Int):Int{
        if(state==-1||state==0||state==1||state==6){
            return View.GONE
        }
        return View.VISIBLE
    }

    fun isShowOrder(state:Int?):Int{

        if(state!=0&&state!=1){
            return View.VISIBLE
        }
        return View.GONE
    }

    fun isNeedPayMoney(order_amount:String?,payment_amount:String?,payment:Int):String{

        if(payment==0){//全款
            return ""
        }
        if(payment==1){//到付
            return "需支付￥$order_amount"
        }

       if(null!=order_amount&&null!=payment_amount){

           return "需支付￥"+DecimalUtils.getNumber(order_amount.toDouble()-payment_amount.toDouble())
       }

        return ""
    }


    fun isShowBut(state: Int?,payment: Int?):Int{//0全款,1=到付,2=付定金


        if(state==5&&(payment==1||payment==2)){
            return View.VISIBLE
        }
        return View.GONE
    }

    fun isButTips(state: Int?):String{
        if(state==5){//司机已到达

            return "确认到达"
        }

        return ""
    }


    fun  isButLoading(state: Int?):Int{

        if(state==3||state==4){
            return View.VISIBLE
        }

        return View.GONE
    }


    fun  getTips(state: Int?):String{
        if(state==2){
            return "司机正在赶往发车点"
        }
        if(state==3||state==4){
            return "司机正在赶往目的地"
        }
        if(state==5){
            return "司机已到达发车点"
        }

        return ""

    }


    fun toCallPhone(mobile: String?) {
        //拨打电话
        if (null!=mobile&&TextUtils.isEmpty(mobile)) {
            toast("该司机未添加电话！！")
            return
        }
        DialUtils.dialPhone(view()?.context(), mobile!!)

    }


    fun toSureOrder(payment: Int?){
        //确定订单
        if(payment==0){//全款

            changeStatus("6")

            return
        }
        if(payment==1){//到付

            UiSwitch.bundleRes(repository().context as Activity, PayActivity::class.java,
                    B().putString("order_id", order_id)
                            .putString("price",bean.value?.actual_amount)
                            .ok(), MIntentAction.REQUEST_CODE_THREE)
        }

        if(payment==2){//付定金
            //todo:
            UiSwitch.bundleRes(repository().context as Activity, PayActivity::class.java,
                    B().putString("order_id", order_id)
                            .putString("price",bean.value?.actual_amount)
                            .ok(), MIntentAction.REQUEST_CODE_THREE)

        }


    }


    fun toCancelOrder(){
        viewModelScope.launch {
           var it= repository().lreq { NetEngine.service.orderCancel(order_id) }
            if(it.ok(true)){

                RxBus.get().post(ConfigRx.REFRESH_LIST, "")

                val intent = Intent()
                var con = repository().context as Activity
                con.setResult(Activity.RESULT_OK, intent)
                con.finish()
            }
        }
    }




   fun getInfo() {
       //订单详情

       viewModelScope.launch {
           var it = repository().preq { NetEngine.service.orderDetail(order_id) }

           if (it.ok()) {
               bean.value=it.data
               state.value=it.data?.status
           }

       }

   }


    var flag=true
    fun  getOrderStatus(){
        flag=false
        viewModelScope.launch {
           var it= repository().lreqTo { NetEngine.service.orderStatus(order_id) }

            if(it.data?.status==1){//等待接单
                flag=true
            }else{//已经接单了
                change.value=1
            }
        }
    }



    fun isRead(id:String?){
        viewModelScope.launch {

            var it = repository().lreqTo { NetEngine.service.msgRead(id) }
            if (it.ok()) {
                RxBus.get().post(ConfigRx.REFRESH_MSG, index)
            }

        }


    }

    fun isMessagePush(){
        viewModelScope.launch {

            var it = repository().lreqTo { NetEngine.service.messagePush(bean.value?.driver_user_id,
                    "货主找你聊天了","你收到货主的聊天信息","com.stars.home.activity.DriverDetailsActivity?order_id="+bean.value?.order_id
            )}

            if (it.ok()) {

            }

        }
    }


    fun changeStatus(status:String){

        viewModelScope.launch {
            var it = repository().lreq { NetEngine.service.updateOrder(order_id, status, null) }
            if (it.ok()) {
                state.value = status!!.toInt()
            }

        }
    }

    fun toLookImg(url: String?) {
        //查看大图

        if(null==url){
            return
        }
        var list = ArrayList<String>()
        list.add(url!!)
//        CCRetrofit.instance()
//                .with(repository().context!!)
//                .create(ILookimg::class.java)
//                .previewImageArr( list, 0)
//                .subscribe()


    }


    //导航
    fun toNavigation(position: String, address: String, lat: String, lng: String) {
        if (null == repository().context) {
            return
        }

        PopuGuideUtils.show(repository().context!!, position, address, lat, lng, ll!!)

    }

}
